/**
 * Created by TEANA on 2018/1/4.
 */
module.exports = ngModule => {
  ngModule.filter('textLengthSet', function () {
    /**
     * @TODO:过滤字符串
     *
     * @param value       (string)  文本内容
     * @param wordwise    (boolean) 是否过滤最后空格' '
     * @param max         (number)  文本最大长度
     * @param tail        (string)  超过长度替换的字符串
     */
    return function(value, wordwise, max, tail) {
      if (!value) return '';

      max = parseInt(max, 10);
      if (!max) return value;
      if (value.length <= max) return value;

      value = value.substr(0, max);
      if (wordwise) {
        var lastspace = value.lastIndexOf(' ');
        if (lastspace != -1) {
          value = value.substr(0, lastspace);
        }
      }

      return value + (tail || ' …');
    };
  });
};
